<template>
  <p-table
    class="table-list-container"
    row-key="id"
    :columns="columns"
    bordered
    v-bind="$attrs"
    v-on="$listeners"
  >
    <a
      slot="orderNo"
      slot-scope="text, record"
      @click="$emit('number-click', record)"
    >
      {{ text }}
    </a>
  </p-table>
</template>

<script>
import { getDicList, postOrganizationInfo } from '@/api/public.js';
import { PLAN_TYPE } from '@/constants/index';

export default {
  props: {},
  data() {
    return {};
  },
  computed: {
    appOptions() {
      return this.$store.state.dictionary.dict['application'] || {};
    },
    orgOptions() {
      return this.$store.getters.getOrg({
        code: 'ltcsmsizebu',
      });
    },
    columns() {
      const columns = [
        {
          align: 'center',
          title: '序号',
          width: 60,
          customRender(t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          align: 'center',
          title: '订单编号',
          dataIndex: 'orderNo',
          width: 150,
          sorter: true,
          ellipsis: true,
          scopedSlots: { customRender: 'orderNo' },
        },
        {
          align: 'center',
          title: '销售组织',
          dataIndex: 'salesOrgCode',
          customRender: (text) => this.orgOptions[text],
          ellipsis: true,
          width: 160,
          sorter: true,
        },
        {
          align: 'center',
          title: '客户简称',
          dataIndex: 'customerName',
          ellipsis: true,
          width: 160,
          sorter: true,
        },
        {
          align: 'center',
          title: '商机编号',
          dataIndex: 'opportunityNo',
          ellipsis: true,
          width: 160,
          sorter: true,
        },
        {
          align: 'center',
          title: '下单法人',
          dataIndex: 'legalName',
          // customRender: (text) => this.stateOptions[text],
          sorter: true,
          ellipsis: true,
          width: 100,
        },
        {
          align: 'center',
          title: 'modelname',
          dataIndex: 'modelName',
          sorter: true,
          ellipsis: true,
          width: 120,
          // customRender: (text) => this.nodeOptions[text],
        },
        {
          align: 'center',
          title: '应用别',
          dataIndex: 'application',
          customRender: (text) => this.appOptions[text],
          sorter: true,
          width: 100,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '数量',
          dataIndex: 'quantity',
          sorter: true,
          ellipsis: true,
          width: 100,
          // customRender: (text) => this.orgOptions[text],
        },
        {
          align: 'center',
          title: '单价',
          dataIndex: 'unitPrice',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '总额',
          dataIndex: 'lumpSum',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '下单时间',
          dataIndex: 'orderTime',
          customRender: (text) => text.substring(0, 10),
          sorter: true,
          width: 100,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '创建人',
          dataIndex: 'createUserName',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '创建时间',
          dataIndex: 'createTime',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '最后修改人',
          dataIndex: 'updateUserName',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
        {
          align: 'center',
          title: '最后修改时间',
          dataIndex: 'updateTime',
          width: 160,
          sorter: true,
          ellipsis: true,
        },
      ];
      return columns;
    },
  },
  async created() {
    this.nodeOptions = await this.getDicList('plan_node');
    this.stateOptions = await this.getDicList('state');
    this.orgOptions = await this.postOrganizationInfo();
  },
  methods: {
    async postOrganizationInfo() {
      const result = await postOrganizationInfo({
        code: 'ltcsmsizebu',
      });
      const data = result?.data?.data || [];
      return data.reduce((pre, cur) => {
        pre[cur.code] = cur.name;
        return pre;
      }, {});
    },
    async getDicList(code) {
      if (!code) return [];
      const result = await getDicList(code);
      const data = result?.data?.data || [];
      return data.reduce((pre, cur) => {
        pre[cur.value] = cur.label;
        return pre;
      }, {});
    },
  },
};
</script>

<style lang="less" scoped>
.table-list-container {
  margin-top: 20px;
}
</style>
